//Copyright (c) 2012 The cwkshell Authors. All rights reserved. 
// Use of this source code is governed by a BSD-style license that can be 
// found in the LICENSE file. 
#include "cuc/src/cwkshell/view/shell_browser_container_non_client_view.h" 


#include <algorithm>

#include "base/basictypes.h"
#include "base/utf_string_conversions.h"
#include "cuc/src/cwkshell/app/shell_property.h" 
#include "cuc/src/cwkshell/browser/shell_browser.h"
#include "cuc/src/cwkshell/common/url_constants.h"
#include "cuc/src/cwkshell/view/shell_browser_container.h"
#include "grit/ui_resources.h"
#include "grit/ui_strings.h"
#include "grit/cwkshell_files_resources.h"
#include "ui/base/hit_test.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/path.h"
#include "ui/views/color_constants.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/grid_layout.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/views/window/client_view.h"
#include "ui/views/window/frame_background.h"
#include "ui/views/window/window_resources.h"
#include "ui/views/window/window_shape.h"

#if defined(USE_AURA)
#include "ui/views/widget/native_widget_aura.h"
#elif defined(OS_WIN)
#include "ui/views/widget/native_widget_win.h"
#endif
namespace cuc{ 
	namespace shell{ 

		namespace {


		


			// The frame border is only visible in restored mode and is hardcoded to 4 px on
			// each side regardless of the system window border size.
			const int kFrameBorderThickness = 2;
			// Various edges of the frame border have a 1 px shadow along their edges; in a
			// few cases we shift elements based on this amount for visual appeal.
			const int kFrameShadowThickness =6;
			// While resize areas on Windows are normally the same size as the window
			// borders, our top area is shrunk by 1 px to make it easier to move the window
			// around with our thinner top grabbable strip.  (Incidentally, our side and
			// bottom resize areas don't match the frame border thickness either -- they
			// span the whole nonclient area, so there's no "dead zone" for the mouse.)
			const int kTopResizeAdjust = 1;
			// In the window corners, the resize areas don't actually expand bigger, but the
			// 16 px at the end of each edge triggers diagonal resizing.
			const int kResizeAreaCornerSize = 16;
			// The titlebar never shrinks too short to show the caption button plus some
			// padding below it.
			
			// The titlebar has a 2 px 3D edge along the top and bottom.
			const int kTitlebarTopAndBottomEdgeThickness = 2;
			// The icon is inset 2 px from the left frame border.
			const int kIconLeftSpacing = 2;
			// The icon never shrinks below 16 px on a side.
			const int kIconMinimumSize = 16;
			// The space between the window icon and the title text.
			const int kTitleIconOffsetX = 4;
			// The space between the title text and the caption buttons.
			const int kTitleCaptionSpacing = 5;



		}  // namespace


		ShellBrowserContainerNonClientView::ShellBrowserContainerNonClientView()
			: frame_(NULL),
			window_icon_(NULL),
			minimize_button_(NULL),
			maximize_button_(NULL),
			restore_button_(NULL),
			close_button_(NULL),
			should_show_minmax_buttons_(false),
			has_title_button_(ShellBrowser::property()->HasTitleButton()),
			current_titlebutton_ (NULL),
			kCaptionButtonHeightWithPadding(
			ShellBrowser::property()->HasTitleButton()?ShellBrowser::property()->GetTitleButtonHeight():
			ShellBrowser::property()->GetTitleBarHeight()),
			kTitleFontName(ShellBrowser::property()->GetTitleButtonFont()),
			kTitleFontSize(ShellBrowser::property()->GetTitleButtonFontSize()),
			frame_background_(new views::FrameBackground()) {
		}

		ShellBrowserContainerNonClientView::~ShellBrowserContainerNonClientView() {
		}

		void ShellBrowserContainerNonClientView::Init(views::Widget* frame) {
			frame_ = frame;

			close_button_ = new views::ImageButton(this);
			close_button_->SetAccessibleName(
				l10n_util::GetStringUTF16(IDS_APP_ACCNAME_CLOSE));

			// Close button images will be set in LayoutWindowControls().
			AddChildView(close_button_);

			minimize_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_MINIMIZE,
				IDR_MINIMIZE, IDR_MINIMIZE_H, IDR_MINIMIZE_P);

			maximize_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_MAXIMIZE,
				IDR_MAXIMIZE, IDR_MAXIMIZE_H, IDR_MAXIMIZE_P);

			restore_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_RESTORE,
				IDR_RESTORE, IDR_RESTORE_H, IDR_RESTORE_P);
			if(has_title_button_){
			
				InitWindowTextButtons();

				InitCurrentTitleButtonFlag();
			
			}

			should_show_minmax_buttons_ = frame_->widget_delegate()->CanMaximize();

			if (frame_->widget_delegate()->ShouldShowWindowIcon()) {
				window_icon_ = new views::ImageButton(this);
				window_icon_->SetImage(views::Button::STATE_NORMAL,
					&(frame_->widget_delegate()->GetWindowIcon()));
				AddChildView(window_icon_);
			}
		}

		///////////////////////////////////////////////////////////////////////////////
		// ShellBrowserContainerNonClientView, NonClientFrameView implementation:

		gfx::Rect ShellBrowserContainerNonClientView::GetBoundsForClientView() const {
			return client_view_bounds_;
		}

		gfx::Rect ShellBrowserContainerNonClientView::GetWindowBoundsForClientBounds(
			const gfx::Rect& client_bounds) const {
				int top_height = NonClientTopBorderHeight();
				int border_thickness = NonClientBorderThickness();
				return gfx::Rect(std::max(0, client_bounds.x() - border_thickness),
					std::max(0, client_bounds.y() - top_height),
					client_bounds.width() + (2 * border_thickness),
					client_bounds.height() + top_height + border_thickness);
		}

		int ShellBrowserContainerNonClientView::NonClientHitTest(const gfx::Point& point) {
			// Sanity check.
			if (!bounds().Contains(point))
				return HTNOWHERE;

			int frame_component = frame_->client_view()->NonClientHitTest(point);

			// See if we're in the sysmenu region.  (We check the ClientView first to be
			// consistent with OpaqueBrowserFrameView; it's not really necessary here.)
			gfx::Rect sysmenu_rect(IconBounds());
			// In maximized mode we extend the rect to the screen corner to take advantage
			// of Fitts' Law.
			if (frame_->IsMaximized())
				sysmenu_rect.SetRect(0, 0, sysmenu_rect.right(), sysmenu_rect.bottom());
			sysmenu_rect.set_x(GetMirroredXForRect(sysmenu_rect));
			if (sysmenu_rect.Contains(point))
				return (frame_component == HTCLIENT) ? HTCLIENT : HTSYSMENU;

			if (frame_component != HTNOWHERE)
				return frame_component;

			// Then see if the point is within any of the window controls.
			if (close_button_->GetMirroredBounds().Contains(point))
				return HTCLOSE;
			if (restore_button_->GetMirroredBounds().Contains(point))
				return HTMAXBUTTON;
			if (maximize_button_->GetMirroredBounds().Contains(point))
				return HTMAXBUTTON;
			if (minimize_button_->GetMirroredBounds().Contains(point))
				return HTMINBUTTON;
			if (window_icon_ && window_icon_->GetMirroredBounds().Contains(point))
				return HTSYSMENU;

			int window_component = GetHTComponentForFrame(point, FrameBorderThickness(),
				NonClientBorderThickness(), kResizeAreaCornerSize, kResizeAreaCornerSize,
				frame_->widget_delegate()->CanResize());
			// Fall back to the caption if no other component matches.
			return (window_component == HTNOWHERE) ? HTCAPTION : window_component;
		}

		void ShellBrowserContainerNonClientView::GetWindowMask(const gfx::Size& size,
			gfx::Path* window_mask) {
				DCHECK(window_mask);
				if (frame_->IsMaximized())
					return;

				views::GetDefaultWindowMask(size, window_mask);
		}

		void ShellBrowserContainerNonClientView::ResetWindowControls() {
			restore_button_->SetState(views::CustomButton::STATE_NORMAL);
			minimize_button_->SetState(views::CustomButton::STATE_NORMAL);
			maximize_button_->SetState(views::CustomButton::STATE_NORMAL);
			// The close button isn't affected by this constraint.
		}

		void ShellBrowserContainerNonClientView::UpdateWindowIcon() {
			if (window_icon_){
				
				window_icon_->SchedulePaint();
				LOG(INFO)<<"ShellBrowserContainerNonClientView::UpdateWindowIcon():DONE";
			}		
		}

		void ShellBrowserContainerNonClientView::UpdateWindowTitle() {
			SchedulePaintInRect(title_bounds_);
		}

		///////////////////////////////////////////////////////////////////////////////
		// ShellBrowserContainerNonClientView, View overrides:

		void ShellBrowserContainerNonClientView::OnPaint(gfx::Canvas* canvas) {
			if (frame_->IsMaximized())
				PaintMaximizedFrameBorder(canvas);
			else
				PaintRestoredFrameBorder(canvas);
			PaintTitleBar(canvas);
			if (ShouldShowClientEdge())
				PaintRestoredClientEdge(canvas);
		}

		void ShellBrowserContainerNonClientView::Layout() {
			LayoutWindowControls();
			LayoutTitleBar();
			LayoutClientView();
		}

		gfx::Size ShellBrowserContainerNonClientView::GetPreferredSize() {
			return frame_->non_client_view()->GetWindowBoundsForClientBounds(
				gfx::Rect(frame_->client_view()->GetPreferredSize())).size();
		}

		///////////////////////////////////////////////////////////////////////////////
		// ShellBrowserContainerNonClientView, ButtonListener implementation:

		void ShellBrowserContainerNonClientView::ButtonPressed(views::Button* sender, const ui::Event& event) {
			if (sender == close_button_)
				frame_->Close();
			else if (sender == minimize_button_)
				frame_->Minimize();
			else if (sender == maximize_button_)
				frame_->Maximize();
			else if (sender == restore_button_)
				frame_->Restore();
			if(has_title_button_)
			TextButtonsPressed(sender,event);
		}

		///////////////////////////////////////////////////////////////////////////////
		// ShellBrowserContainerNonClientView, private:

		int ShellBrowserContainerNonClientView::FrameBorderThickness() const {
			return frame_->IsMaximized() ? -2 : kFrameBorderThickness;
		}

		int ShellBrowserContainerNonClientView::NonClientBorderThickness() const {
			// In maximized mode, we don't show a client edge.
			return FrameBorderThickness() +
				(ShouldShowClientEdge() ? kClientEdgeThickness : 0);
		}

		int ShellBrowserContainerNonClientView::NonClientTopBorderHeight() const {
			return std::max(FrameBorderThickness() + IconSize(),
				CaptionButtonY() + kCaptionButtonHeightWithPadding) +
				TitlebarBottomThickness();
		}

		int ShellBrowserContainerNonClientView::CaptionButtonY() const {
			// Maximized buttons start at window top so that even if their images aren't
			// drawn flush with the screen edge, they still obey Fitts' Law.
			return frame_->IsMaximized() ? FrameBorderThickness() : kFrameShadowThickness;
		}

		int ShellBrowserContainerNonClientView::TitlebarBottomThickness() const {
			return kTitlebarTopAndBottomEdgeThickness +
				(ShouldShowClientEdge() ? kClientEdgeThickness : 0);
		}

		int ShellBrowserContainerNonClientView::IconSize() const {
#if defined(OS_WIN)
			// This metric scales up if either the titlebar height or the titlebar font
			// size are increased.
			return GetSystemMetrics(SM_CYSMICON);
#else
			return std::max(GetTitleFont().GetHeight(), kIconMinimumSize);
#endif
		}

		gfx::Rect ShellBrowserContainerNonClientView::IconBounds() const {
			int size = IconSize();
			int frame_thickness = FrameBorderThickness();
			// Our frame border has a different "3D look" than Windows'.  Theirs has a
			// more complex gradient on the top that they push their icon/title below;
			// then the maximized window cuts this off and the icon/title are centered
			// in the remaining space.  Because the apparent shape of our border is
			// simpler, using the same positioning makes things look slightly uncentered
			// with restored windows, so when the window is restored, instead of
			// calculating the remaining space from below the frame border, we calculate
			// from below the 3D edge.
			int unavailable_px_at_top = frame_->IsMaximized() ?
frame_thickness : kTitlebarTopAndBottomEdgeThickness;
			// When the icon is shorter than the minimum space we reserve for the caption
			// button, we vertically center it.  We want to bias rounding to put extra
			// space above the icon, since the 3D edge (+ client edge, for restored
			// windows) below looks (to the eye) more like additional space than does the
			// 3D edge (or nothing at all, for maximized windows) above; hence the +1.
			int y = unavailable_px_at_top + (NonClientTopBorderHeight() -
				unavailable_px_at_top - size - TitlebarBottomThickness() + 1) / 2;
			return gfx::Rect(frame_thickness + kIconLeftSpacing, y, size, size);
		}

		bool ShellBrowserContainerNonClientView::ShouldShowClientEdge() const {
			return !frame_->IsMaximized();
		}

		void ShellBrowserContainerNonClientView::PaintRestoredFrameBorder(gfx::Canvas* canvas) {
			frame_background_->set_frame_color(GetFrameColor());
			const gfx::ImageSkia* frame_image = GetFrameImage();
			frame_background_->set_theme_image(frame_image);
			frame_background_->set_top_area_height(frame_image->height());

			ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
			
			frame_background_->SetCornerImages(
				rb.GetImageNamed(IDR_AURA_SHADOW_ACTIVE_TOP_LEFT).ToImageSkia(),
				rb.GetImageNamed(IDR_AURA_SHADOW_ACTIVE_TOP_RIGHT).ToImageSkia(),
				rb.GetImageNamed(IDR_AURA_SHADOW_ACTIVE_BOTTOM_LEFT).ToImageSkia(),
				rb.GetImageNamed(IDR_AURA_SHADOW_ACTIVE_BOTTOM_RIGHT).ToImageSkia());
		
			frame_background_->SetSideImages(
				rb.GetImageNamed(IDR_AURA_SHADOW_ACTIVE_LEFT).ToImageSkia(),
				rb.GetImageNamed(IDR_AURA_SHADOW_ACTIVE_TOP).ToImageSkia(),
				rb.GetImageNamed(IDR_AURA_SHADOW_ACTIVE_RIGHT).ToImageSkia(),
				rb.GetImageNamed(IDR_AURA_SHADOW_ACTIVE_BOTTOM).ToImageSkia());
				
			frame_background_->PaintRestored(canvas, this);
		}

		void ShellBrowserContainerNonClientView::PaintMaximizedFrameBorder(gfx::Canvas* canvas) {
			const gfx::ImageSkia* frame_image = GetFrameImage();
			frame_background_->set_theme_image(frame_image);
			frame_background_->set_top_area_height(frame_image->height());
			frame_background_->PaintMaximized(canvas, this);

			ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();

			// TODO(jamescook): Migrate this into FrameBackground.
			// The bottom of the titlebar actually comes from the top of the Client Edge
			// graphic, with the actual client edge clipped off the bottom.
			const gfx::ImageSkia* titlebar_bottom = rb.GetImageNamed(
				IDR_APP_TOP_CENTER).ToImageSkia();
			int edge_height = titlebar_bottom->height() -
				(ShouldShowClientEdge() ? kClientEdgeThickness : 0);
			canvas->TileImageInt(*titlebar_bottom, 0,
				frame_->client_view()->y() - edge_height, width(), edge_height);
		}

		void ShellBrowserContainerNonClientView::PaintTitleBar(gfx::Canvas* canvas) {
			views::WidgetDelegate* delegate = frame_->widget_delegate();

			// It seems like in some conditions we can be asked to paint after the window
			// that contains us is WM_DESTROYed. At this point, our delegate is NULL. The
			// correct long term fix may be to shut down the RootView in WM_DESTROY.
			if (!delegate)
				return;
			if(!has_title_button_){
				canvas->DrawStringInt(delegate->GetWindowTitle(), GetTitleFont(),
#ifdef CWKSHELL_SKIN_GRAY
					SkColorSetRGB(23,25,16), GetMirroredXForRect(title_bounds_),
#else
					SK_ColorWHITE, GetMirroredXForRect(title_bounds_),
#endif
					title_bounds_.y(), title_bounds_.width(),
					title_bounds_.height());
			}
		
		}

		void ShellBrowserContainerNonClientView::PaintRestoredClientEdge(gfx::Canvas* canvas) {
			gfx::Rect client_area_bounds = frame_->client_view()->bounds();
			int client_area_top = client_area_bounds.y();

			ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
			const gfx::ImageSkia* top_left = rb.GetImageNamed(
				IDR_APP_TOP_LEFT).ToImageSkia();
			const gfx::ImageSkia* top = rb.GetImageNamed(
				IDR_APP_TOP_CENTER).ToImageSkia();
			const gfx::ImageSkia* top_right = rb.GetImageNamed(
				IDR_APP_TOP_RIGHT).ToImageSkia();
			const gfx::ImageSkia* right = rb.GetImageNamed(
				IDR_CONTENT_RIGHT_SIDE).ToImageSkia();
			const gfx::ImageSkia* bottom_right = rb.GetImageNamed(
				IDR_CONTENT_BOTTOM_RIGHT_CORNER).ToImageSkia();
			const gfx::ImageSkia* bottom = rb.GetImageNamed(
				IDR_CONTENT_BOTTOM_CENTER).ToImageSkia();
			const gfx::ImageSkia* bottom_left = rb.GetImageNamed(
				IDR_CONTENT_BOTTOM_LEFT_CORNER).ToImageSkia();
			const gfx::ImageSkia* left = rb.GetImageNamed(
				IDR_CONTENT_LEFT_SIDE).ToImageSkia();

			int client_area_bottom =
				std::max(client_area_top, client_area_bounds.bottom());

			// Top.
			int top_edge_y = client_area_top - top->height();
			canvas->DrawImageInt(*top_left, client_area_bounds.x() - top_left->width(),
				top_edge_y);
			canvas->TileImageInt(*top, client_area_bounds.x(), top_edge_y,
				client_area_bounds.width(), top->height());
			canvas->DrawImageInt(*top_right, client_area_bounds.right(), top_edge_y);

			// Right.
			
			int client_area_height = client_area_bottom - client_area_top;
			canvas->TileImageInt(*right, client_area_bounds.right(), client_area_top,
				right->width(), client_area_height);

			// Bottom.
			canvas->DrawImageInt(*bottom_right, client_area_bounds.right(),
				client_area_bottom);
			canvas->TileImageInt(*bottom, client_area_bounds.x(), client_area_bottom,
				client_area_bounds.width(), bottom_right->height());
			canvas->DrawImageInt(*bottom_left,
				client_area_bounds.x() - bottom_left->width(), client_area_bottom);

			// Left.
			canvas->TileImageInt(*left, client_area_bounds.x() - left->width(),
				client_area_top, left->width(), client_area_height);
			
			// Draw the color to fill in the edges.
			canvas->FillRect(gfx::Rect(client_area_bounds.x() - 1, client_area_top - 1,
				client_area_bounds.width() + 1, client_area_bottom - client_area_top + 1),
				views::kClientEdgeColor);

		}

		SkColor ShellBrowserContainerNonClientView::GetFrameColor() const {
			  return kDefaultColorFrame ;
		}

		const gfx::ImageSkia* ShellBrowserContainerNonClientView::GetFrameImage() const {
			return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
#ifdef CWKSHELL_SKIN_BLACK
				 IDR_FILES_UI_FRAME_DEFAULT_BLACK_JPG ).ToImageSkia();
#endif
#ifdef CWKSHELL_SKIN_BLUE
			 IDR_FILES_UI_FRAME_DEFAULT_BLUE_PNG).ToImageSkia();
#endif
#ifdef CWKSHELL_SKIN_GRAY
			IDR_FILES_UI_FRAME_DEFAULT_GRAY_PNG ).ToImageSkia();
#endif
		}

		void ShellBrowserContainerNonClientView::LayoutWindowControls() {
			close_button_->SetImageAlignment(views::ImageButton::ALIGN_LEFT,
				views::ImageButton::ALIGN_BOTTOM);
			int caption_y = CaptionButtonY();
			bool is_maximized = frame_->IsMaximized();
			// There should always be the same number of non-shadow pixels visible to the
			// side of the caption buttons.  In maximized mode we extend the rightmost
			// button to the screen corner to obey Fitts' Law.
			int right_extra_width = is_maximized ?
				(kFrameBorderThickness - kFrameShadowThickness) : 0;
			gfx::Size close_button_size = close_button_->GetPreferredSize();
			close_button_->SetBounds(width() - FrameBorderThickness() -
				right_extra_width - close_button_size.width(), caption_y,
				close_button_size.width() + right_extra_width,
				close_button_size.height());

			// When the window is restored, we show a maximized button; otherwise, we show
			// a restore button.
			bool is_restored = !is_maximized && !frame_->IsMinimized();
			views::ImageButton* invisible_button = is_restored ? restore_button_
				: maximize_button_;
			invisible_button->SetVisible(false);

			views::ImageButton* visible_button = is_restored ? maximize_button_
				: restore_button_;
			views::FramePartImage normal_part, hot_part, pushed_part;
			if (should_show_minmax_buttons_) {
				visible_button->SetVisible(true);
				visible_button->SetImageAlignment(views::ImageButton::ALIGN_LEFT,
					views::ImageButton::ALIGN_BOTTOM);
				gfx::Size visible_button_size = visible_button->GetPreferredSize();
				visible_button->SetBounds(close_button_->x() - visible_button_size.width(),
					caption_y, visible_button_size.width(),
					visible_button_size.height());

				minimize_button_->SetVisible(true);
				minimize_button_->SetImageAlignment(views::ImageButton::ALIGN_LEFT,
					views::ImageButton::ALIGN_BOTTOM);
				gfx::Size minimize_button_size = minimize_button_->GetPreferredSize();
				minimize_button_->SetBounds(
					visible_button->x() - minimize_button_size.width(), caption_y,
					minimize_button_size.width(),
					minimize_button_size.height());

				normal_part = IDR_CLOSE;
				hot_part = IDR_CLOSE_H;
				pushed_part = IDR_CLOSE_P;
			} else {
				visible_button->SetVisible(false);
				minimize_button_->SetVisible(false);

				normal_part = IDR_CLOSE_SA;
				hot_part = IDR_CLOSE_SA_H;
				pushed_part = IDR_CLOSE_SA_P;
			}

			ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();

			close_button_->SetImage(views::CustomButton::STATE_NORMAL,
				rb.GetImageNamed(normal_part).ToImageSkia());
			close_button_->SetImage(views::CustomButton::STATE_HOVERED,
				rb.GetImageNamed(hot_part).ToImageSkia());
			close_button_->SetImage(views::CustomButton::STATE_PRESSED,
				rb.GetImageNamed(pushed_part).ToImageSkia());
		}

		void ShellBrowserContainerNonClientView::LayoutTitleBar() {
			// The window title position is calculated based on the icon position, even
			// when there is no icon.
			gfx::Rect icon_bounds(IconBounds());
			bool show_window_icon = window_icon_ != NULL;
			if (show_window_icon)
				window_icon_->SetBoundsRect(icon_bounds);

			// The offset between the window left edge and the title text.
			int title_x = show_window_icon ? icon_bounds.right() + kTitleIconOffsetX
				: icon_bounds.x();
			int title_height = GetTitleFont().GetHeight();
			// We bias the title position so that when the difference between the icon and
			// title heights is odd, the extra pixel of the title is above the vertical
			// midline rather than below.  This compensates for how the icon is already
			// biased downwards (see IconBounds()) and helps prevent descenders on the
			// title from overlapping the 3D edge at the bottom of the titlebar.
		
			
			title_bounds_.SetRect(title_x,
				icon_bounds.y() + ((icon_bounds.height() - title_height - 1) / 2),
				std::max(0, (should_show_minmax_buttons_ ?
				minimize_button_->x() : close_button_->x()) - kTitleCaptionSpacing -
				title_x), title_height);
			if(has_title_button_){
				LayoutTextButtons();
				LayoutCurrentTitleButtonFlag();
			}
		}

		void ShellBrowserContainerNonClientView::LayoutClientView() {
			int top_height = NonClientTopBorderHeight();
			int border_thickness = NonClientBorderThickness();
			client_view_bounds_.SetRect(border_thickness, top_height,
				std::max(0, width() - (2 * border_thickness)),
				std::max(0, height() - top_height - border_thickness));
		}

		views::ImageButton* ShellBrowserContainerNonClientView::InitWindowCaptionButton(
			int accessibility_string_id,
			int normal_image_id,
			int hot_image_id,
			int pushed_image_id) {
				ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
				views::ImageButton* button = new views::ImageButton(this);
				button->SetAccessibleName(l10n_util::GetStringUTF16(accessibility_string_id));
				button->SetImage(views::CustomButton::STATE_NORMAL,
					rb.GetImageNamed(normal_image_id).ToImageSkia());
				button->SetImage(views::CustomButton::STATE_HOVERED,
					rb.GetImageNamed(hot_image_id).ToImageSkia());
				button->SetImage(views::CustomButton::STATE_PRESSED,
					rb.GetImageNamed(pushed_image_id).ToImageSkia());
				AddChildView(button);
				return button;
		}

		void ShellBrowserContainerNonClientView::InitWindowTextButtons()
		{
			ShellProperty* property = ShellBrowser::instance()->GetShellProperty();
			ShellProperty::TitleUrls title_urls =  property->GetTitleUrls();
			ShellProperty::TitleUrls::iterator it = title_urls.begin();
			for(;it != title_urls.end();it++){
				ShellProperty::TitleUrl title_url = (*it);
				views::TextButton* text_button = new views::TextButton(this,title_url.first);
				std::string url = title_url.second;
				UrlTextButton url_textbutton = std::make_pair(text_button,url);
				text_button->set_focusable(true);
			//	text_button->SetTextShadowColors(SK_ColorLTGRAY,SK_ColorBLACK);
				text_button->SetEnabledColor(SK_ColorWHITE);

				text_button->SetHoverColor(SK_ColorWHITE);
					//	text_button->SetHoverColor(SkColorSetRGB(136,211,24));
				text_button->SetFont(GetTitleFont());
				url_text_buttons_.push_back(url_textbutton);
				AddChildView(text_button);
			}
			
			current_titlebutton_ = url_text_buttons_.at(0).first;
		}

		void ShellBrowserContainerNonClientView::LayoutTextButtons()
		{
			UrlTextButtons::iterator it = url_text_buttons_.begin();
			//int x = title_bounds_.x()+50;
			int x = 30;
			for ( ;it != url_text_buttons_.end();it++)
			{
				
				views::TextButton* p = (*it).first;
				int height = p->font().GetHeight();
				int width = p->font().GetStringWidth(p->text());
				int page_title_font_height = views::NativeWidgetWin::GetWindowTitleFont().GetHeight();
				gfx::Rect rect(x,page_title_font_height+5,width+20,kCaptionButtonHeightWithPadding-30);
				x += (width+40);
				p->SetBoundsRect(rect);		
			}
		}

		
		void ShellBrowserContainerNonClientView::TextButtonsPressed(
			views::Button* sender, const ui::Event& event )
		{
			UrlTextButtons::iterator it = url_text_buttons_.begin();
			for (;it != url_text_buttons_.end();it++)
			{
				if((*it).first == sender ){
					ShellBrowser::instance()->GetShellBrowserContainer()->LoadURL(GURL((*it).second));
					UpdateCurrentTitleButtonFlag(sender);
				}
			}
		}

		const gfx::Font& ShellBrowserContainerNonClientView::GetTitleFont()
		{
			static gfx::Font* title_font = NULL;
			if (!title_font) {
				//title_font = new gfx::Font(views::NativeWidgetWin::GetWindowTitleFont());
				title_font = new gfx::Font(kTitleFontName,kTitleFontSize);
			}
			return *title_font;
		}

		void ShellBrowserContainerNonClientView::InitCurrentTitleButtonFlag()
		{
			current_titlebutton_flag_ = new views::View;
			current_titlebutton_flag_->set_background(
				views::Background::CreateSolidBackground(SkColorSetRGB(76,184,72)));
			AddChildView(current_titlebutton_flag_);

		}

		void ShellBrowserContainerNonClientView::LayoutCurrentTitleButtonFlag()
		{
			gfx::Rect rc = current_titlebutton_->bounds();
			int x = rc.x();
			int y = rc.y() + rc.height();
			int height = kCaptionButtonHeightWithPadding - y;
			int width = rc.width();
			current_titlebutton_flag_->SetBoundsRect(gfx::Rect(x,y,width,height));
			
		}

		void ShellBrowserContainerNonClientView::
			UpdateCurrentTitleButtonFlag(views::Button* current_titlebutton)
		{
			current_titlebutton_ = current_titlebutton;
			LayoutCurrentTitleButtonFlag();
		}


	} //namespace shell 
} //namespace cuc 
